package service;

import dao.CommonDAO;
import dao.CourseDao;
import entity.Modular;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PermissionService {

    CommonDAO cd = new CommonDAO();

    CourseDao courseDao = new CourseDao();

    public List<Modular> modularList(){
        List<Modular> list=new ArrayList<>();
        String sql="select * from modular";
        List<Map<String,Object>> temp=cd.executeQuery(sql,new Object[0]);

        for(int i=0;i<temp.size();i++){
            Map<String,Object> map=temp.get(i);
            list.add(new Modular(
                    map.get("mName").toString(),
                    Integer.parseInt(map.get("modularId").toString())
            ));
        }
        return list;
    }

    public List<Modular> modularsByCharactersID(Integer id){
        List<Modular> list=new ArrayList<>();
        String sql="select mName,modularId from permission ,modular where permission.modular=modular.modularId and`characters` = "+id;
        List<Map<String,Object>> temp=cd.executeQuery(sql,new Object[0]);

        for(int i=0;i<temp.size();i++){
            Map<String,Object> map=temp.get(i);
            list.add(new Modular(
                    map.get("mName").toString(),
                    Integer.parseInt(map.get("modularId").toString())
            ));
        }
        return list;
    }

    public void updateModular(String characterId, String permissions){
        courseDao.updateModular(characterId, permissions);
    }

}
